<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/5/19
 * Time: 11:00
 */
namespace Admin\Controller;
use Think\Controller;

class ActivityController extends CommonController{

    protected function _initialize(){
        parent::_initialize();
        $this->breadcrumb1='活动管理';
        $this->breadcrumb2='消息推送';
        $this->url=U('Activity/index');
        $this->user_id=session('user_auth')['uid'];
    }

    public function index(){
        $menu_id=$_GET['id'];
        $jur=jur_str($menu_id,$this->user_id);



        $search=I('post.search','','string');
        if($search){
              $where['title']=array('like',"%$search%");
        }
        $where['status']=array('neq',3);
        $count=M('notice')->where($where)->count();
        $Page       = new \Think\Page($count,25);
        $notice=M("notice")->where($where)->order('create_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $show       = $Page->show();
        $this->assign('notice',$notice);// 赋值数据集
        $this->assign('update',$jur['is_update']);// 赋值数据集
        $this->assign('delete',$jur['is_delete']);// 赋值数据集
        $this->assign('search',$search);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    /**
     * 添加推送
     */
    public function add(){
        if($_POST){
            $username= session('user_auth');
            $data['title']=I("post.title");
            $data['type']=I("post.type");
            $data['content']=I("post.content");
            $data['create_time']=time();
            $data['status']=1;
            $data['create_user']=$username['username'];
            $a=M('notice')->add($data);
            $userList=M('user')->select();
            foreach($userList as $k=>$v){
                $udata['user_id']=$v['user_id'];
                $udata['message_id']=$a;
                M('messuser')->add($udata);
            }
         if($a){
             $jpush['alias']='all';
             $jpush['content']=$data['title'];
             $jpush['data']=$data['data'];
             $jpush['m_time']=60*60*24;
             $jpush=serialize($jpush);
             $redis = S(array('type' => 'redis'));
             //得到推送信息
             $jpush = $redis->lpush(C('JPUSH_KEY'),$jpush);
         }
            echo 1;exit;
        }else{
            $this->display();
        }
    }




    public function index_cancel(){
      if($_POST){
          $redis = S(array('type' => 'redis'));
          //得到推送信息

          $data['alias']='all';
          $data['content']=I("post.content");
          $data['data']='';
          $data['m_time']=600;
          $str=serialize($data);
          $redis->lpush(C('JPUSH_KEY'),$str);
          echo 1;exit;
      }else{
         $this->display();
      }

    }

    /**
     * 编辑
     */
    public function edit(){
       if($_POST){
           $data['title']=I("post.title");
           $data['type']=I("post.type");
           $data['content']=I("post.content");
           $data['status']=I("post.status");
           $where['id']=I("post.id");
           $code=M('notice')->where($where)->save($data);
           if($code===false){
               echo 0;exit;
           }elseif($code==0){
               echo 1;exit;
           }else{
               echo 2;exit;
           }
       }else{
           $id=I("get.id",'','int');
           $detail=M('notice')->where("id=$id")->find();
           // var_dump($detail);exit;
           $this->assign('detail',$detail);
           $this->display();
       }

    }

    /**
     * 删除
     */
    public function delete(){
          $ids=I('post.ids');
          $ids=implode(',',$ids);
          $where['id']=array('in',$ids);
         $save['status']=3;
         M('notice')->where($where)->save($save);
    }

    /**
     *编辑
     */
    public function inedit(){
      var_dump($_POST);exit;

    }
}